package com.example.barry.pdf.Utils;

import org.ksoap2.serialization.SoapObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;

/**
 * Created by barry on 17-2-15.
 */

public class Help {

    /**
     * 解析SoapObject对象
     */
    /**
     *
     *
     * @param result
     * @return
     */
    public  String[] parseSoapObject(SoapObject result) {

        String string = result.toString();
        String str = string.substring(40,string.length()-3);
        String[] strings = null;
        strings = str.split("@");
        return strings;

    }
    public String[] parseQuerySerialInfo(SoapObject result){
        String string = result.toString();
        String str = string.substring(53,string.length()-3);
        String[] strings = null;
        strings = str.split("; ");
        return strings;
    }

    /**
     *
     * 通过path 解析url
     * @param urlPath
     * @return
     * @throws Exception
     */

    public String getJsonString(String urlPath) throws Exception {
        URL url = new URL(urlPath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        //对应的字符编码转换
        Reader reader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        String str = null;
        StringBuffer sb = new StringBuffer();
        while ((str = bufferedReader.readLine()) != null) {
            sb.append(str);
        }
        reader.close();
        connection.disconnect();
        return sb.toString();
    }

    /**
     *
     * 获取本地IP
     * @return
     */
    public static String getIpAddressString() {
        try {
            for (Enumeration<NetworkInterface> enNetI = NetworkInterface
                    .getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
                NetworkInterface netI = enNetI.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = netI
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }
}
